package com.hy.study;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * 拦截bean的创建过程
 *
 * @author huyong
 * @date 2024 年 01 月 03 日 10:32: 52
 */
@Component
public class TestBeanPostProcessor implements BeanPostProcessor {

    private final static Logger log = Logger.getLogger(TestBeanPostProcessor.class);

    /**
     * 在 init 方法之前调用
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        log.info("TestBeanPostProcess .......postProcessBeforeInitialization :"+beanName );
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

    /**
     * 在init 方法之后调用
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        log.info("TestBeanPostProcess .......postProcessAfterInitialization :"+beanName );
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
}
